package com.yksj.healthtalk.ui.doctorstation;

import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.TimeUtil;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 患者列表          (服务内容中点击的患者列表)
 * @author Administrator
 *
 */
public class DoctorServiceHistoryClientListActivity extends BaseFragmentActivity implements OnClickListener {

	private PullToRefreshListView refreshListView;
	private ListView mListView;
	private MyAdapter adapter;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctorservice_history_client_activity_layout);
		initView();
	}

	private void initView() {
		initTitle();
		titleTextV.setText("服务中的患者");
		titleLeftBtn.setOnClickListener(this);
		refreshListView = (PullToRefreshListView)findViewById(R.id.listview_fragment);
		adapter = new MyAdapter(this);
		mListView = refreshListView.getRefreshableView();
		TextView textView=new TextView(this);
		textView.setText("还没有患者哦");
		textView.setGravity(Gravity.CENTER);
		mListView.setEmptyView(textView);
		mListView.setAdapter(adapter);
		initData();
	}
	
	private void initData() {
		RequestParams requestParams=new RequestParams();
		requestParams.put("customerId",SmartFoxClient.getLoginUserId());
		requestParams.put("contentId",getIntent().getStringExtra("SERVICE_CONTENT_ID"));
		HttpRestClient.doHttpFindPatientsByContentId(requestParams,new JsonsfHttpResponseHandler(this){
			private LodingFragmentDialog showLodingDialog;
			
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				super.onSuccess(statusCode, response);
				adapter.changeData(response);
			}
			
			@Override
			public void onFailure(Throwable e, JSONArray errorResponse) {
				super.onFailure(e, errorResponse);
				ToastUtil.showBasicErrorShortToast(getApplicationContext());
			}
		});
		
	}

	class MyAdapter extends BaseAdapter{
		private JSONArray array=new JSONArray();
		private final ImageLoader loader;
		private final LayoutInflater from;
		
		public MyAdapter(Context con) {
			this.loader=ImageLoader.getInstance();
			from = LayoutInflater.from(con);
		}
		public void changeData(JSONArray array){
			this.array=array;
			notifyDataSetChanged();
		}
		@Override
		public int getCount() {
			return array.size();
		}

		@Override
		public JSONObject getItem(int position) {
			return array.getJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			JSONObject obj=getItem(position);
			ViewHolder holder;
			if(convertView==null){
				holder=new ViewHolder();
				convertView=from.inflate(R.layout.doctor_service_history_client_list_item, null);
				holder.name=(TextView) convertView.findViewById(R.id.name);
				holder.buyTime=(TextView) convertView.findViewById(R.id.buy_time);
				holder.endTime=(TextView) convertView.findViewById(R.id.end_time);
				holder.type=(TextView) convertView.findViewById(R.id.type);
				holder.view=(ImageView) convertView.findViewById(R.id.view);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			if("".equals(obj.getString("REMARKS_NAME"))){
				holder.name.setText(obj.getString("CUSTOMER_ACCOUNTS"));
			}else{
				holder.name.setText(obj.getString("REMARKS_NAME"));
			}
			
			holder.buyTime.setText("开始时间:"+TimeUtil.format(obj.getString("SERVICE_START")));
			holder.endTime.setText("截止时间:"+TimeUtil.format(obj.getString("SERVICE_END")));
			
			holder.type.setText(obj.getString("SERVICE_TYPE"));
			loader.displayImage(obj.getString("CLIENT_ICON_BACKGROUND"), holder.view);
			return convertView;
		}
		
		class ViewHolder{
			TextView name;
			TextView buyTime;
			TextView endTime;
			TextView type;
			ImageView view;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
}
